<html>
<head><meta charset="utf-8"><title>Design of Duration · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html">Design of Duration</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="186400243"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186400243" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186400243">(Jan 23 2020 at 15:35)</a>:</h4>
<p>Hey, not sure if general is the best channel for this or not. I was curious about the design of Duration. Full disclosure, I'm a C++ dev that follows Rust here and there. Some conversation led me to look at the Duration struct. It seems concerning to me because by its design, it's not going to usable in very high performance applications. In C++ on the other hand, if you look at chrono's approach, you end up with a Duration type that can be a zero cost abstraction. I'm curious what the reasoning was for such a design?</p>



<a name="186400361"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186400361" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186400361">(Jan 23 2020 at 15:36)</a>:</h4>
<p>what does chrono do differently?</p>



<a name="186400440"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186400440" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186400440">(Jan 23 2020 at 15:37)</a>:</h4>
<p>Sure, so I guess I'll give the background on both. C++ Duration is templated on basically a numeric type, which is stored, and a Ratio type. The ratio type isn't stored, but simply provides, statically, constants that tell you in what units the type is "counting" duration. It does this by providing a ratio to seconds.</p>



<a name="186400608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186400608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186400608">(Jan 23 2020 at 15:38)</a>:</h4>
<p>In many high performance applications, what you typically do is simply use 64 bit integers as nanoseconds since epoch. The C++ duration type is fully compatible with this approach, and when compiled with optimizations, operations on durations will have the same codegen as if you did it using simple integers, i.e. it's a zero cost abstraction.</p>



<a name="186400655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186400655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186400655">(Jan 23 2020 at 15:39)</a>:</h4>
<p><span class="user-mention" data-user-id="260061">@Nir Friedman</span> I believe <code>Duration</code>'s design was made long before <code>chrono</code> was released.</p>



<a name="186400897"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186400897" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186400897">(Jan 23 2020 at 15:41)</a>:</h4>
<p>The rust approach is to store seconds in a 64 bit integer, and nanos in a 32 bit integer. This is simply hardcoded, which already has some disadvantages. However, in addition, it's hardcoded to something which is big (96 bytes), and also because the nanos portion needs to be kept under 1 billion, adding two durations becomes a surprisingly expensive operation. You have to add the nanos, check if the value is over a billion, if yes, then subtract a billion and add one to the seconds... etc</p>



<a name="186401052"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186401052" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186401052">(Jan 23 2020 at 15:43)</a>:</h4>
<p><span class="user-mention" data-user-id="219696">@XAMPPRocky</span> chrono was released in 2011. Is there an easy way to see when std:<span aria-label="time" class="emoji emoji-1f557" role="img" title="time">:time:</span>:Duration was added?</p>



<a name="186401062"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186401062" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186401062">(Jan 23 2020 at 15:43)</a>:</h4>
<p>Well... it would seem to me like there could be a generic version that <code>Duration</code> is just one concrete impl of</p>



<a name="186401249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186401249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186401249">(Jan 23 2020 at 15:45)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> yeah, the other thing I was going to point out is if you really badly want both massive precision and massive range, then nothing is stopping you from templating the C++ chrono on a 96 bit integer type. I'm kind of surprised that Rust avoided generics in this situation.</p>
<p>One thing I don't know for sure is whether Rust's type system can do what C++ does with ratio, i.e. you can have constants associated with a type? I <em>think</em> Rust can but I'm not certain.</p>



<a name="186401334"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186401334" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186401334">(Jan 23 2020 at 15:45)</a>:</h4>
<p>You can indeed have constants associated with a type. I think prototyping a Duration that does so outside of core/std makes sense; indeed, we may want to merge it in if it works well.</p>



<a name="186401489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186401489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186401489">(Jan 23 2020 at 15:46)</a>:</h4>
<p>(potentially, we could do so backwards compatibly, as <span class="user-mention" data-user-id="124288">@oli</span> suggests)</p>



<a name="186401716"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186401716" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186401716">(Jan 23 2020 at 15:49)</a>:</h4>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> interesting. Well, I'm not that familiar with Rust (nor honestly have the time), but if someone is interested in prototyping something like that, would be happy to talk to them, explain a  bit about chrono. I think it does serve an important use case, because people in e.g. HFT simply wouldn't be able to use a Duration type like in Rust</p>



<a name="186401757"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186401757" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186401757">(Jan 23 2020 at 15:49)</a>:</h4>
<p>Boost had a similar type (96 bit) back in the day and it was heavily avoided in HFT (high frequency trading) communities</p>



<a name="186401835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186401835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186401835">(Jan 23 2020 at 15:50)</a>:</h4>
<p>I'd assume it would be similar in other fields where very low latency/high performance is necessary</p>



<a name="186401841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186401841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186401841">(Jan 23 2020 at 15:50)</a>:</h4>
<p><span class="user-mention" data-user-id="260061">@Nir Friedman</span> From researching std's duration was chrono's original duration design. Here's a link to the initial commit and the design reform.</p>
<p><a href="https://github.com/rust-lang/rust/commit/5778ed4c926da6069ab899c8d4694527ed89190c" target="_blank" title="https://github.com/rust-lang/rust/commit/5778ed4c926da6069ab899c8d4694527ed89190c">https://github.com/rust-lang/rust/commit/5778ed4c926da6069ab899c8d4694527ed89190c</a><br>
<a href="https://github.com/rust-lang/rfcs/blob/master/text/1040-duration-reform.md" target="_blank" title="https://github.com/rust-lang/rfcs/blob/master/text/1040-duration-reform.md">https://github.com/rust-lang/rfcs/blob/master/text/1040-duration-reform.md</a></p>



<a name="186401919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186401919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186401919">(Jan 23 2020 at 15:50)</a>:</h4>
<blockquote>
<p>Boost had a similar type (96 bit) back in the day and it was heavily avoided in HFT (high frequency trading) communities</p>
</blockquote>
<p><span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span> That sounds like a feature, not a bug</p>



<a name="186401955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186401955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186401955">(Jan 23 2020 at 15:51)</a>:</h4>
<p>What was the feature? Sorry don't follow the joke</p>



<a name="186401969"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186401969" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186401969">(Jan 23 2020 at 15:51)</a>:</h4>
<p>Making life hard for HFT ppl</p>



<a name="186402161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186402161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186402161">(Jan 23 2020 at 15:53)</a>:</h4>
<p>That's kinda mean, unfunny, and off topic, all at once</p>



<a name="186402302"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186402302" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186402302">(Jan 23 2020 at 15:54)</a>:</h4>
<p><span class="user-mention" data-user-id="219696">@XAMPPRocky</span> Looking at this RFC, there's an additional pretty major limitation here, which I didn't realize, which is that Duration has to always be positive?</p>



<a name="186402404"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186402404" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186402404">(Jan 23 2020 at 15:55)</a>:</h4>
<p>I haven't read either two, I just found them.</p>



<a name="186402545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186402545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186402545">(Jan 23 2020 at 15:56)</a>:</h4>
<p>That doesn't really make much sense, especially once you start thinking about e.g. subtracting time_point's; subtracting two points in time can easily give you a negative duration.</p>



<a name="186402620"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186402620" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186402620">(Jan 23 2020 at 15:57)</a>:</h4>
<p>As I understand, Duration is only for comparison between <code>SystemTime</code>s, it's not for general date duration.</p>



<a name="186402623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186402623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186402623">(Jan 23 2020 at 15:57)</a>:</h4>
<p>I think the argument is that distance can't be negative either</p>



<a name="186403209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186403209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186403209">(Jan 23 2020 at 16:02)</a>:</h4>
<p>I don't really think that's much of an argument; distance is non-negative by definition because it's a magnitude. Displacement has direction, which in one dimension means signedness. You can always recover distance from displacement, but not vice versa. So you need to have a concept of the more general thing.</p>
<p>At any rate I don't think that such analogies add much to technical analysis, generally. But <span class="user-mention" data-user-id="219696">@XAMPPRocky</span> , I see what you are saying, to me it seems odd that a second duration type would be needed.</p>



<a name="186403288"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186403288" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186403288">(Jan 23 2020 at 16:03)</a>:</h4>
<p>I'm not going to argue about it. It's not my design, and I don't particularly care. I'm just providing context.</p>



<a name="186403504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186403504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186403504">(Jan 23 2020 at 16:04)</a>:</h4>
<p>Do you know if there's more review or discussion of that RFC? I thought such discussions happened on github issues, I did a search there and couldn't find anything</p>



<a name="186403640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186403640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186403640">(Jan 23 2020 at 16:06)</a>:</h4>
<p><a href="https://github.com/rust-lang/rfcs/pull/1040" target="_blank" title="https://github.com/rust-lang/rfcs/pull/1040">https://github.com/rust-lang/rfcs/pull/1040</a></p>



<a name="186403923"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186403923" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186403923">(Jan 23 2020 at 16:09)</a>:</h4>
<p>interesting, thanks. Seems like people are divided over whether it's intended to be used as part of a datetime API or not (or at least, there's 2 people arguing over it).</p>



<a name="186403976"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186403976" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186403976">(Jan 23 2020 at 16:09)</a>:</h4>
<p>I think duration is also non-negative by definition. At least in natural languages you can't say this took "-5 minutes"</p>



<a name="186404170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186404170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186404170">(Jan 23 2020 at 16:11)</a>:</h4>
<p>Note that there's also the <code>chrono</code> crate, and there may be others that fit the design you envision better</p>



<a name="186404282"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186404282" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186404282">(Jan 23 2020 at 16:12)</a>:</h4>
<p>Again, I don't think analogies (to natural language) are buying you much. If you want to have a duration type that's part of a datetime library, you need to be able to subtract two time_points and have a sensible result, regardless of the order of that subtraction.</p>



<a name="186404318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186404318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186404318">(Jan 23 2020 at 16:12)</a>:</h4>
<p>If you look at prior art (including Joda), 100% of Duration-like types I've seen in various languages support being negative</p>



<a name="186404345"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186404345" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186404345">(Jan 23 2020 at 16:13)</a>:</h4>
<p>I've looked at chrono, IIRC the standard Duration is based on it, but I could be mixed up</p>



<a name="186404364"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186404364" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186404364">(Jan 23 2020 at 16:13)</a>:</h4>
<p>I think it's the other way around</p>



<a name="186404386"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186404386" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186404386">(Jan 23 2020 at 16:13)</a>:</h4>
<p><code>chrono::Duration</code> may be a reexport of <code>std::time::Duration</code></p>



<a name="186404393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186404393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186404393">(Jan 23 2020 at 16:13)</a>:</h4>
<p>Ah maybe you are right. chrono::Duration allows negatives, so that's a start for sure.</p>



<a name="186404397"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186404397" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186404397">(Jan 23 2020 at 16:13)</a>:</h4>
<p><span class="user-mention" data-user-id="260061">@Nir Friedman</span> <code>Duration</code> is not part of a datetime library. It's only use in std is with <code>SystemTime::duration_since</code>, where it panics if you have a negative duration.</p>



<a name="186404477"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186404477" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186404477">(Jan 23 2020 at 16:14)</a>:</h4>
<p><span class="user-mention" data-user-id="219696">@XAMPPRocky</span> Sure, I agree, Rust's Duration is not currently, it's just not clear from that thread whether that's the intention moving forward or not.</p>



<a name="186404562"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186404562" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186404562">(Jan 23 2020 at 16:15)</a>:</h4>
<p>That said, even in that example, I don't think duration_since's behavior is very reasonable (and again, certainly inconsistent with prior art)</p>



<a name="186406501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186406501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186406501">(Jan 23 2020 at 16:30)</a>:</h4>
<p>It's a result, not a panic, and the error has a duration value but it's just a "kinda negative" duration.</p>
<p>So really, "duration is always positive" is just weird design all the way through</p>



<a name="186407410"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/186407410" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nir Friedman <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#186407410">(Jan 23 2020 at 16:38)</a>:</h4>
<p>Yeah. I don't think that "it's reasonably common to only want positive Durations", is reason to limit your main Duration type to just positive. What rust calls <code>SystemTime::now().duration_since(some_other_time)</code> is basically just subtraction of two time points, no matter how you spin it. And you need negative durations for subtraction of time_points to make any sense, and for obvious and desirable identities to hold.</p>



<a name="187163198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/187163198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#187163198">(Feb 01 2020 at 14:00)</a>:</h4>
<p>FWIW when I just wanted to do some simple time stuff, I found C++'s maze of templates incredibly hard to navigate, and was pretty happy that Rust's libstd didn't follow that design.^^ I am sure there is a place for a high-performance lib with a complex API, but I am not sure if libstd is that place.</p>



<a name="187166183"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Design%20of%20Duration/near/187166183" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Design.20of.20Duration.html#187166183">(Feb 01 2020 at 15:40)</a>:</h4>
<p>FWIW Duration <em>is</em> zero cost in a sense that it’s effectively the same thing as the system API (timespec)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>